<?php
    #-------------------------------------------------------------#
	# This file is part of the Kumomado - Virtual Window Platform #
	# for PHP. (c) 2009 Tsai, Min Che <tirme0812@gmail.com>       #
	# Please visit http://www.kumomado.org/ for more information. #
	#                                                             #
	# This software is licensed under the GNU License v3. Please  #
	# view the LICENSE file for the full text of this license.    #
    #-------------------------------------------------------------#

	Kumomado::load('Kui.Components.Base.KumomadoUIBase');
	/**
	 * @package libraries/kui/components/base/
	 * @author Tsai, Min Che
	 * @version 1.0
	 */
	class KumomadoCheckBox extends KumomadoUIBase {
		/**
		 * @var string
		 */
		protected $ui_name = 'KCheckbox';
		/**
		 * @var array
		 */
		protected $options;
		/**
		 * @var boolean
		 */
		protected $break = false;
		/**
		 * @param string $id
		 * @param array $options
		 * @param boolean $break
		 */
		public function __construct($id, $options = null, $break = false) {
			$this->setId($id);
			$this->addOptions($options);
			$this->setBreak();
		}
		/**
		 * @param string $value
		 * @param string $text
		 * @param boolean $checked
		 */
		public function addOption($value, $text, $checked = false) {
			$this->options[] = array(
				'value' => $value,
				'text' => $text,
				'checked' => $checked
			);
		}
		/**
		 * @param array $options
		 * @return boolean
		 */
		public function addOptions($options) {
			if (!is_array($options)) {
				return false;
			}
			foreach ($options as $option) {
				if (isset($option['value']) && isset($option['text'])) {
					$checked = isset($option['checked']) ? (boolean)$option['checked'] : false;
					$this->addOption($option['value'], $option['text'], $checked);
				}
			}
			return true;
		}
		/**
		 * @param boolean $break
		 */
		public function setBreak($break = false) {
			$this->break = (boolean)$break;
		}
		/**
		 * Get attributes
		 * @return array
		 */
		public function getAttrs() {
			return array(
				'id' => $this->getId(),
				'options' => $this->options
			);
		}
		/**
		 * Get html view
		 * @return string
		 */
		public function view() {
			$view = '';
			foreach ($this->options as $option) {
				$view .= '<input type="checkbox" name="'.$this->getId().'[]" value="'.$option['value'].'" '.(true === $option['checked'] ? 'checked="true"' : '').' />'.$option['text'];
				if ($this->break) {
					$view .= '<br />';
				}
			}
			return $view;
		}
		/**
		 * @return array 
		 */
		public function render() {
			return array(
				'ui_name' => $this->ui_name,
				'attrs' => $this->getAttrs(),
				'view' => $this->view()
			);
		}
	}
?>